package com.lenin.omena.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.HelpPanel.State;
import com.lenin.omena.client.SlidingAnimation.Direction;

public class MainPanel extends Composite {
	
	interface MyUiBinder extends UiBinder<Widget, MainPanel> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	@UiField
	AbsolutePanel headerPanelContainer;
	
	@UiField
	AbsolutePanel mapPanelContainer;
	
	private MapPanel mapPanel = new MapPanel();
	
	private HeaderPanel headerPanel;
	
	private HelpPanel helpPanel;
	
	private LinksPanel linksPanel;
	
	private DialogPopup dialogPopup;
	
	
	public MainPanel() {
		
		initWidget(uiBinder.createAndBindUi(this));
		
		int windowHeight = Window.getClientHeight();
		int windowWidth = Window.getClientWidth();
		
		headerPanelContainer.setWidth(Window.getClientWidth()+"px");
		headerPanelContainer.getElement().getStyle().setOverflow(Overflow.VISIBLE);
		
		linksPanel = new LinksPanel();
		linksPanel.setWidth("300px");
		linksPanel.setHeight("90px");
		linksPanel.getElement().getStyle().setZIndex(0);
		
		headerPanelContainer.add(linksPanel, (windowWidth-310), 0);
		
		helpPanel = new HelpPanel();
		helpPanel.setWidth(500);
		helpPanel.setHeight(50);
		
		int initLeft = (windowWidth-helpPanel.getWidth())/2;
		int initTop = windowHeight-(helpPanel.getHeight());
		
		headerPanelContainer.add(helpPanel, initLeft, initTop);
		helpPanel.setHeight(250);
		helpPanel.setTop(windowHeight-250);
		
		headerPanel = new HeaderPanel();
		headerPanel.getElement().getStyle().setZIndex(3000);
		headerPanelContainer.add(headerPanel);
		
		mapPanel.setMainPanel(this);
		mapPanelContainer.add(mapPanel);
		
		Window.addResizeHandler(new ResizeHandler() {
			
			public void onResize(ResizeEvent resizeEvent) {
				
				adjustMapContainerHeight();
				
			}
			
		});
		
		
		helpPanel.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				
				SlidingAnimation anim;
				
				int windowHeight = Window.getClientHeight();
				
				if(helpPanel.getState() == State.CLOSED) {
					anim = new SlidingAnimation(helpPanel, windowHeight, 20, 250, Direction.UP);
					helpPanel.setState(State.OPEN);
				} else {
					anim = new SlidingAnimation(helpPanel, windowHeight, 20, 250, Direction.DOWN);
					helpPanel.setState(State.CLOSED);
				}
				
				anim.run(500);
			
			}
			
		});
		
	}
	
	
	public boolean showDialogPopup() {
		
		if(dialogPopup == null) {
			
			dialogPopup = new DialogPopup();
			dialogPopup.setVisible(false);
			dialogPopup.getElement().getStyle().setZIndex(4000);
			
			headerPanelContainer.add(dialogPopup, 200, 200);
			
		}
		
		if(!dialogPopup.isVisible()) {
		
			dialogPopup.setVisible(true);
		
			int left = (Window.getClientWidth()-dialogPopup.getOffsetWidth())/2;
			int top = (Window.getClientHeight()-dialogPopup.getOffsetHeight())/3;
		
			dialogPopup.getElement().getStyle().setTop(top, Unit.PX);
			dialogPopup.getElement().getStyle().setLeft(left, Unit.PX);
			
			return true;
			
		} else {
			
			return false;
			
		}
		
	}
	
	
	public boolean hideDialogPopup() {
		
		if(dialogPopup != null && dialogPopup.isVisible()) {
			
			dialogPopup.setVisible(false);
			return true;
			
		} else {
			
			return false;
		
		}
		
	}
	
	
	public AbsolutePanel getHeaderContainer() {
		return headerPanelContainer;
	}
	
	public MapPanel getMapPanel() {
		return mapPanel;
	}
	
	public void adjustMapContainerHeight() {
		
		int windowHeight = Window.getClientHeight();
		int windowWidth = Window.getClientWidth();
		int headerHeight = headerPanelContainer.getOffsetHeight();
		int mapHeight = windowHeight - headerHeight;
		
		headerPanelContainer.setWidth(Window.getClientWidth()+"px");
		
		mapPanelContainer.setHeight(mapHeight+"px");
		
		mapPanel.centerMap();
		
		adjustHelpPanelGeometry();
		
	}
	
	
	public void adjustHelpPanelGeometry() {
		
		int windowHeight = Window.getClientHeight();
		int windowWidth = Window.getClientWidth();
		
		int helpLeft = (windowWidth-helpPanel.getWidth())/2;
		int helpTop = windowHeight-(helpPanel.getHeight()-20);
		helpPanel.setLeft(helpLeft);
		helpPanel.setTop(helpTop);
		
	}
	
	
	public void setHelpWidget(Widget widget) {
		helpPanel.setContent(widget);
	}
	
	
	
}
